$(function () {
    let d1 = $('.top')
    let str1 = "石林仁济医院"
    circle_font(d1, str1)

    let sql = `
    select
    d.code,
    床号,a.住院号,姓名,性别,年龄,入院诊断 诊断,费用,
    b.dept 科室,
    case 入院日期 when DATE (NOW()) then '新入' else 住院天数  end 住院天数,
    入院日期,
    手术日期
    from
    patients a
    left join
    employee b on a.管床医生=b.name
    left join
    assistant_doctor c on a.住院号=c.住院号
    left join
    employee d on c.医助=d.name
    left join 
    operation e on a.住院号=e.住院号
    where (出院日期 is null) and b.dept = '微创'
    order by 床号    
    `
    let elementClass = '.content'
    getDomData(sql, elementClass, table)

    $.post('/sql', { sql: `select * from (select dept ,sum(case when 出院日期 is null then 1 else 0 end) as 人数, sum(case when 出院日期=date(now()) then 1 else 0 end) as 出院 ,sum(case when 入院日期=date(now()) then 1 else 0 end) as 入院 from patients a left join employee b on a.管床医生=b.name   group by dept having 人数>0 or 出院>0 order by dept desc) t3 union select '全院',sum(case when 出院日期 is null then 1 else 0 end) as 人数, sum(case when 出院日期=date(now()) then 1 else 0 end) as 出院 ,sum(case when 入院日期=date(now()) then 1 else 0 end) as 入院 from patients a left join employee b on a.管床医生=b.name` }, function (datas) {
        let data = datas[1]
        let result = ''
        for (let i = 0; i < data.length; i++) {
            result += `
            <div name='${data[i][0]}'>
                <span>出${data[i][2]}</span>
                ${data[i][0]}${data[i][1]}人
                <i>入${data[i][3]}</i>
            </div>
            `
            $(document.querySelector('.nav_title')).empty().append(result)
        }
    })

    $('.nav_title').on('click', 'div', function () {
        getDomData($(this).attr('name'), elementClass, table)
        $(this).siblings().removeClass('back2')
        $(this).addClass('back2')

    })

    $('.aside div:nth-child(2)').on('click', function () {
        $.get('/数据更新', function () { })
        // wrapper_content()
        // wrapper_title()
        $(this).siblings().removeClass('back2')
        $(this).addClass('back2')
    })

    $('.aside').on('click', 'div', function () {
        $(this).siblings().removeClass('back2')
        $(this).addClass('back2')
    })

    function medical2() {
        $('.nav_title').empty().append(`<div class='back1'>血糖公式</div>`)
        $('.content').empty().append(`<div>
    <label for="bg">血糖（mmol/L）</label>
    <input id="bg" type="text" name="bg" value="18"><br>
    <label for="bw">体重（kg）</label>
    <input id="bw" type="text" name="bw" value="50"><br>
    <label for="result">胰岛素用量（UI）</label>
    <input type="text" name="result" readonly>    
</div>
`)
        blood_glucose()
    }
    $('.aside div:nth-child(9)').on('click', medical2)
    $('.content').on('input', 'input', blood_glucose)
})